package com.centaur0.tags;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import com.centaur0.ui.menu.EstruturaMenu;
import com.centaur0.ui.menu.GeneralMenuStyle;
import com.centaur0.ui.menu.Menu;
import com.centaur0.ui.menu.XmlMenuConfigParser;
import com.centaur0.util.Application;

public class MenuTagBKP extends SimpleTagSupport {
	public String path;
	public String div_id;
	
	public String getDiv_id() {
		return div_id;
	}

	public void setDiv_id(String div_id) {
		this.div_id = div_id;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	@Override
	public void doTag() throws JspException, IOException {
		ServletContext svlCtx = Application.getHttpApplicationContext();
		File file = new File(svlCtx.getRealPath(path));
		EstruturaMenu em = null;
		try{
			em = XmlMenuConfigParser.parseDoc(file);
		} catch(Exception e){
			throw new JspException(e);
		}
		this.printHtml(em);
	}
	
	private void printHtml(EstruturaMenu em) throws JspException, IOException{
		StringBuilder out = new StringBuilder();
		GeneralMenuStyle gms = em.getGeneralMenuStyle();
		boolean flagInnerCellStyle;
		boolean flagLabelStyle;
		
		if((flagInnerCellStyle = (gms.getInnerCellStyleId() != null && !gms.getInnerCellStyleId().isEmpty())) |
				(flagLabelStyle = (gms.getLabelStyleId() != null && !gms.getLabelStyleId().isEmpty()))){
			out.append("<style type='text/css'>\n");
				if(flagInnerCellStyle){
					out.append("\n".concat(gms.getInnerCellStyle()));
				}
				if(flagLabelStyle){
					out.append("\n".concat(gms.getLabelStyle()));
				}
			out.append("</style>");
		}
		List<Menu> menus = em.getMenus();
		boolean hasInnerMenu = false;
		out.append(String.format("<div id='%s'>\n\t", div_id == null? em.toString().substring(em.toString().indexOf("@")+1): div_id));
			out.append("<table style='border:0px;' id='OBJ_Menu'>\n\t\t");
				out.append("<tr id='OBJ_Menu'>\n\t\t");
				StringBuilder buffer = new StringBuilder("<style type='text/css'>\n");
				for(Menu menu: menus){
					String labelStyleId = "_NENHUM_LABELSTYLE_".concat(Long.toString(System.currentTimeMillis()));
					if(menu.getLabelStyleId() != null && !menu.getLabelStyleId().isEmpty()){
						labelStyleId = menu.getLabelStyleId();
						buffer.append("\n".concat(menu.getLabelStyle()));
					} else if(flagLabelStyle){
						labelStyleId = gms.getLabelStyleId();
					}
					
					String innerCellStyleId = "_NENHUM_INNERCELLSTYLE".concat(Long.toString(System.currentTimeMillis()));
					if(menu.getInnerCellStyleId() != null && !menu.getInnerCellStyleId().isEmpty()){
						innerCellStyleId  = menu.getInnerCellStyleId();
						buffer.append("\n".concat(menu.getInnerCellStyle()));
					} else if(flagInnerCellStyle){
						innerCellStyleId = gms.getInnerCellStyleId();
					}
					
					out.append("<td style='border:0px;width:auto' id='OBJ_Menu'>\n\t\t\t");
						out.append(String.format("<div id='%s'>\n\t\t\t\t", innerCellStyleId));
							if(menu.getChildren() != null && menu.getChildren().size() > 0){
								hasInnerMenu = true;
								this.construirInnerMenus(flagLabelStyle, flagInnerCellStyle, out, menu, gms);
							}
							if(menu.getLink() != null && menu.getLink().isAjax()){
								AjaxLinkTag tag = new AjaxLinkTag();
								tag.setJspContext(getJspContext());
								tag.setParent(this);
								tag.setAguarde(menu.getLink().getAguarde());
								tag.setDescription(menu.getLabel());
								tag.setFundoEscuro(menu.getLink().getFundoEscuro());
								tag.setHrefId(labelStyleId);
								tag.setOuterId(menu.getLink().getOuterId());
								tag.setPath(menu.getLink().getText());
								tag.doTag();
							} else {
								out.append(String.format("<a id='%s' href='%s'", labelStyleId, (menu.getLink() == null || menu.getLink().getText().isEmpty()? 
										"javascript:;":menu.getLink().getText())).concat(">\n\t\t\t\t\t"));
									out.append("&nbsp;".concat(menu.getLabel()).concat("&nbsp;\n\t\t\t\t"));
								out.append("</a>\n\t\t\t");
							}
						out.append("</div>\n\t\t");
					out.append("</td>\n\t\t");
				}
				buffer.append("\n</style>");
				out.append("</tr>\n\t");
			out.append("</table>\n");
		out.append("</div>\n");
		if(hasInnerMenu){
			out.append("<script>");
				out.append("var divs  = new Array();");
				out.append("var index = 0;");
				out.append("var funcaoCallBack = function(){");
					out.append("if(divs != null && divs.length > 0){");
						out.append("for(i = 0; i < divs.length; i++){");
							out.append("document.getElementById(divs[i]).style.display = 'none';");
						out.append("}");
					out.append("}");
				out.append("};");
				out.append("function adicionarDivParaBloqueio(divId){");
					out.append("if(divs != null && divs.length > 0){");
						out.append("var x = false;");
						out.append("for(var i in divs){");
							out.append("if(divs[i] == divId){");
								out.append("x = true;");
								out.append("break;");
							out.append("}");
						out.append("}");
						out.append("if(!x){");
							out.append("divs[index++] = divId;");
						out.append("}");
					out.append("} else divs[index++] = divId;");
				out.append("}");
				out.append("var functionOnClick = function(){");
					out.append("eval('funcaoCallBack();');");		
				out.append("};");
			out.append("</script>");
		}
		this.getJspContext().getOut().append(buffer.toString());
		this.getJspContext().getOut().append(out.toString());
	}

	private void construirInnerMenus(boolean flagLabelStyle, boolean flagInnerCellStyle, 
			StringBuilder out, Menu menuParent, GeneralMenuStyle gms) 
	throws JspException, IOException{
		out.append(String.format("<div id='%s'>\n\t", div_id == null? menuParent.toString().substring(menuParent.toString().indexOf("@")+1): div_id));
			out.append("<table style='border:0px;' id='OBJ_Menu'>\n\t\t");
				out.append("<tr id='OBJ_Menu'>\n\t\t");
				StringBuilder buffer = new StringBuilder("<style type='text/css'>\n");
				List<Menu> menus = menuParent.getChildren();
				for(Menu menu: menus){
					String labelStyleId = "_NENHUM_LABELSTYLE_".concat(Long.toString(System.currentTimeMillis()));
					if(menu.getLabelStyleId() != null && !menu.getLabelStyleId().isEmpty()){
						labelStyleId = menu.getLabelStyleId();
						buffer.append("\n".concat(menu.getLabelStyle()));
					} else if(flagLabelStyle){
						labelStyleId = gms.getLabelStyleId();
					}
					
					String innerCellStyleId = "_NENHUM_INNERCELLSTYLE".concat(Long.toString(System.currentTimeMillis()));
					if(menu.getInnerCellStyleId() != null && !menu.getInnerCellStyleId().isEmpty()){
						innerCellStyleId  = menu.getInnerCellStyleId();
						buffer.append("\n".concat(menu.getInnerCellStyle()));
					} else if(flagInnerCellStyle){
						innerCellStyleId = gms.getInnerCellStyleId();
					}
					
					out.append("<td style='border:0px;width:auto' id='OBJ_Menu'>\n\t\t\t");
						out.append(String.format("<div id='%s'>\n\t\t\t\t", innerCellStyleId));
							if(menu.getChildren() != null && menu.getChildren().size() > 0){
								this.construirInnerMenus(flagLabelStyle, flagInnerCellStyle, out, menu, gms);
							}
							if(menu.getLink() != null && menu.getLink().isAjax()){
								AjaxLinkTag tag = new AjaxLinkTag();
								tag.setJspContext(getJspContext());
								tag.setParent(this);
								tag.setAguarde(menu.getLink().getAguarde());
								tag.setDescription(menu.getLabel());
								tag.setFundoEscuro(menu.getLink().getFundoEscuro());
								tag.setHrefId(labelStyleId);
								tag.setOuterId(menu.getLink().getOuterId());
								tag.setPath(menu.getLink().getText());
								tag.doTag();
							} else {
								out.append(String.format("<a id='%s' href='%s'", labelStyleId, (menu.getLink() == null || menu.getLink().getText().isEmpty()? 
										"javascript:;":menu.getLink().getText())).concat(">\n\t\t\t\t\t"));
									out.append("&nbsp;".concat(menu.getLabel()).concat("&nbsp;\n\t\t\t\t"));
								out.append("</a>\n\t\t\t");
							}
						out.append("</div>\n\t\t");
					out.append("</td>\n\t\t");
				}
				buffer.append("\n</style>");
				out.append("</tr>\n\t");
			out.append("</table>\n");
		out.append("</div>\n");
	}
}
